package huanzhao.erp.domain.po;

import com.fasterxml.jackson.annotation.JsonIgnore;
import huanzhao.erp.domain.enumerate.error.BaseErrorCode;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author April
 * 2024/7/25 上午10:56
 * @version 1.0
 */
@Data
@AllArgsConstructor
public class Result<T> {
    private int code;
    private String message;

    /**
     * 本次请求携带的数据
     * 若data为null则不写入到json字符串中
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public Result(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> Result<T> error(BaseErrorCode errorCode) {
        return new Result<>(errorCode.getCode(), errorCode.getMessage());
    }

    public static <T> Result<T> error(String message) {
        return new Result<>(-1, message);
    }

    public static <T> Result<T> error(String message, T data) {
        return new Result<>(-1, message, data);
    }

    public static <T> Result<T> error(BaseErrorCode errorCode, String message) {
        return new Result<>(errorCode.getCode(), message);
    }

    public static <T> Result<T> ok() {
        return new Result<>(0, "请求成功");
    }

    public static <T> Result<T> ok(String message) {
        return new Result<>(0, message);
    }

    public static <T> Result<T> ok(T data) {
        return new Result<>(0, "请求成功", data);
    }

    public static <T> Result<T> ok(String message, T data) {
        return new Result<>(0, message, data);
    }

    /**
     * 判断本次结果是否为错误
     *
     * @return boolean
     * 2024/8/5 下午7:36:49
     */
    @JsonIgnore
    public boolean isError() {
        return code != 0;
    }
}